-
Notifications
You must be signed in to change notification settings - Fork 84
feat: output full bson objects MONGOSH-1285 #2584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
| this: DeepInspectServiceProviderWrapper, | ||
| ...args: Parameters<Required<ServiceProvider>[K]> | ||
| ): // eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
| // @ts-ignore The returntype already contains a promise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A way around all this is to just make one function similar to bsonMethod() for each unique return type, kinda like what I did for the cursor methods.
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
2428a91 to
4b2991c
Compare
| undefined, | ||
| undefined, | ||
| initialServiceProvider | ||
| this.initialServiceProvider |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw it is VERY easy to accidentally pass initialServiceProvider (ie. the unwrapped value) to something in place of this.initialServiceProvider. Ask me how I know..
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
| for (const item of obj) { | ||
| addCustomInspect(item); | ||
| } | ||
| } else if ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe better to reuse an isSimpleObject() test like https://github.com/mongodb-js/devtools-shared/blob/ff08cc5e55212890f1d8635f545474201b1e1167/packages/mongodb-redact/src/utils.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements deep inspection for BSON objects returned from the database by wrapping the ServiceProvider to intercept and modify document inspection behavior. When BSON data is retrieved, it recursively installs custom inspect functions that override Node.js's default truncation limits for depth, array length, and string length.
Key changes:
- Introduced
DeepInspectServiceProviderWrapperand cursor wrappers to intercept database responses - Added
addCustomInspectfunction that recursively attaches custom inspection behavior to BSON documents - Updated
ShellInstanceStateto conditionally wrap service providers based on platform compatibility
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/shell-api/src/shell-instance-state.ts | Wraps the initial service provider with deep inspect functionality when supported |
| packages/shell-api/src/shell-api.spec.ts | Adds missing test setup properties for service provider stub |
| packages/shell-api/src/runtime-independence.spec.ts | Updates test assertions to account for wrapped service provider |
| packages/shell-api/src/pick-methods-by-return-type.ts | Defines utility type for filtering methods by return type |
| packages/shell-api/src/deep-inspect-service-provider-wrapper.ts | Implements service provider wrapper that intercepts cursor and BSON-returning methods |
| packages/shell-api/src/deep-inspect-run-command-cursor-wrapper.ts | Wraps run command cursors to apply custom inspect to results |
| packages/shell-api/src/deep-inspect-find-cursor-wrapper.ts | Wraps find cursors to apply custom inspect to documents |
| packages/shell-api/src/deep-inspect-change-stream-wrapper.ts | Wraps change streams to apply custom inspect to change events |
| packages/shell-api/src/deep-inspect-aggregation-cursor-wrapper.ts | Wraps aggregation cursors to apply custom inspect to results |
| packages/shell-api/src/custom-inspect.ts | Implements recursive custom inspection logic for BSON documents |
| packages/service-provider-core/src/service-provider.ts | Adds deepInspectWrappable interface property and default implementation |
| packages/service-provider-core/src/admin.ts | Updates getNewConnection return type to ServiceProvider |
| packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/JavaServiceProvider.kt | Disables deep inspect wrapping for Java shell platform |
| packages/e2e-tests/test/e2e-oidc.spec.ts | Updates test to access wrapped service provider via _sp property |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
packages/shell-api/src/deep-inspect-service-provider-wrapper.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <[email protected]>
addaleax
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Should I try to take a stab at trying to deduplicate the cursor implementations a bit?
If we don't do that, I think I'd have a mild preference for grouping all the deep-* files in a shared subdirectory, since they're a fairly isolated thing of their own
Co-authored-by: Anna Henningsen <[email protected]>
… instead of class
| expect(output).to.include('foundme'); | ||
| expect(output).to.include('num: 99999'); | ||
| expect(output).to.include('The End'); | ||
| // Same object doesn't need to be fully printed if created by the user |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
addaleax
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just approving this for Github, but I'm not going to merge without approval from @lerouxb on my own changes in this PR 🙂
MONGOSH-1285
I built this on top of @addaleax's wip branch, so the types are still mostly from there.
The short version of what we're trying to do: For BSON that we get from the database we want to print all of it, untruncated, when it gets evaluated or otherwise inspected with util.inspect().
This solution wraps the ServiceProvider with another class that implements all the same methods. Then:
I have been testing it with this document which should be affected by the inspectOptions
depth,maxArrayLengthandmaxStringLength:And this doc has every BSON type which is useful for testing that we're not messing up existing BSON formatting:
You can test that the find cursor's tryNext was replaced by running:
Notice that the whole array, string and object all printed. However, this specific case already worked before because we special-case printing cursors and a few other things.
This will exercise the inspect function on the top-level array that was returned.
You can test that it recursively installed it on the documents inside that array by running:
or even
This needs a lot of tests. There are almost certainly some cases left that I've missed. And I'm unsure about some details. Just opening to have a discussion. Oh and we might want a way for users to opt out of it.